import axios from 'axios'
import store from '@/store/index'
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
interface YYRequestInterceptors<T = AxiosResponse> {
  requestInterceptors?: (res: AxiosRequestConfig) => AxiosRequestConfig
  requestInterceptorsCatch?: (err: any) => any
  responseInterceptors?: (res: T) => T
  responseInterceptorsCatch?: (err: any) => any
}
interface YYAxiosRequestConfig<T = AxiosResponse> extends AxiosRequestConfig {
  interceptors?: YYRequestInterceptors<T>
  showLoading?: boolean
}
const DEFAULTLoading = false

class YYRequest {
  instance: AxiosInstance
  interceptors?: YYRequestInterceptors
  showLoading: boolean
  constructor(config: YYAxiosRequestConfig) {
    this.instance = axios.create(config)
    //将实例化的四个拦截器赋值给interceptors,
    this.interceptors = config.interceptors
    //默认是有遮罩层的
    this.showLoading = config.showLoading ?? DEFAULTLoading
    // 实例中有interceptors这个对象,里面包含了四个拦截器
    this.instance.interceptors.request.use(
      //因为有四个拦截器所以需要分开使用
      //直接到参数对象里面找也是可以的
      config.interceptors?.requestInterceptors,
      config.interceptors?.requestInterceptorsCatch
    )
    this.instance.interceptors.response.use(
      this.interceptors?.responseInterceptors,
      this.interceptors?.responseInterceptorsCatch
    )

    //所有实例都默认有的拦截器
    this.instance.interceptors.request.use(
      (config) => {
        // this.nextTick(() => {
        //   // Loading should be closed asynchronously
        //   loadingInstance.close()
        // })
        if (this.showLoading) {
          store.commit('showLoading')
        }
        return config
      },
      (err) => {
        return err
      }
    )
    this.instance.interceptors.response.use(
      (res) => {
        const data = res.data
        //如果请求的响应的字段是-1001则也是请求失败,对此进行拦截
        // if (data.returnCode === '-1001') {
        //   console.log('请求失败')
        // } else {
        setTimeout(() => {
          store.commit('hideLoading')
        }, 1000)
        return data
        // }
      },
      (err) => {
        setTimeout(() => {
          store.commit('hideLoading')
        }, 500)
        //对不同的响应错误代码，进行不同的显示
        switch (err.response.status) {
          case 404:
            console.log('404的错误')
        }
        return err
      }
    )
  }
  // 单独请求
  //request请求
  request<T>(config: YYAxiosRequestConfig<T>): Promise<T> {
    return new Promise((resolve, reject) => {
      //请求对应的拦截对config进行处理然后返回出去
      if (config.interceptors?.requestInterceptors) {
        config = config.interceptors.requestInterceptors(config)
      }
      //判断是否需要定义loading
      if (config.showLoading) {
        this.showLoading = config.showLoading
      }
      //对服务器进行请求
      this.instance
        .request<any, T>(config)
        .then((res) => {
          //响应拦截器对请求的响应内容进行处理并返回
          if (config.interceptors?.responseInterceptors) {
            res = config.interceptors.responseInterceptors(res)
          }
          resolve(res)
          //将showLoading再次修改为false，不影响下次亲求
          this.showLoading = DEFAULTLoading
        })
        .catch((err) => {
          this.showLoading = DEFAULTLoading
          reject(err)
          return err
        })
    })
  }
  //get 请求
  get<T>(config: YYAxiosRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'GET' })
  }
  //post 请求
  post<T>(config: YYAxiosRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'POST' })
  }
  //delete 请求
  delete<T>(config: YYAxiosRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'DELETE' })
  }
  //patch 请求
  patch<T>(config: YYAxiosRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'PATCH' })
  }
}

export default YYRequest
